#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN DATA ABSTRACTION LAYER version 1

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using System.Reflection;
using System.ComponentModel;
using Thn.Data.Exceptions;
#endregion

namespace Thn.Data.Mapping
{
    /// <summary>
    /// Provides mapping between object attribute and data attribute.
    /// <para>And also provide method to Get Value and Set Value using reflection</para>
    /// </summary>
    public class ReflectionAttributeMap: AttributeMap
    {
        FieldInfo fi = null;
        PropertyInfo pi = null;

        #region Get Value
        /// <summary>
        /// Retrieve the object's attribute value
        /// </summary>
        public object GetValue(object target)
        {
            object result = null;
            //use reflection to obtain value
            if (fi != null) result = fi.GetValue(target);
            else result = pi.GetValue(target, null);
            //custom serialization in special cases
            object temp = result;
            try
            {
                if (IsList || IsArray)
                {
                    result = this.Serialize(temp);
                }
                else
                {
                    //this type does not have its own converter, try to serialize it
                    TypeConverter con = TypeDescriptor.GetConverter(this.ObjectAttributeType);
                    if (con == null || con.GetType() == typeof(System.ComponentModel.TypeConverter) ||
                        con.GetType() == typeof(System.ComponentModel.ReferenceConverter))
                    {
                        result = this.Serialize(temp);
                    }
                }
            }
            catch (Exception ex)
            {
                CanNotSerializableException.Publish(temp);
            }

            return result;
        }        
        #endregion

        #region Set value
        /// <summary>
        /// Assign value to object's attribute
        /// </summary>
        public void SetValue(object target, object value)
        {
            string text = value as string;
            // Thuy Anh

            //custom serialization in special cases
            try
            {
                if (IsList || IsArray)
                {
                    value = this.Deserialize(text);
                }
                else
                {
                    //this type does not have its own converter, try to serialize it
                    TypeConverter con = TypeDescriptor.GetConverter(this.ObjectAttributeType);
                    //if (con is TypeConverter)
                    if (con == null || con.GetType() == typeof(System.ComponentModel.TypeConverter) ||
                        con.GetType() == typeof(System.ComponentModel.ReferenceConverter))
                    {
                        value = this.Deserialize(text);
                    }
                }
            }
            catch (Exception ex)
            {
                CanNotDeserializableException.Publish(text);
            }

            if (fi != null) fi.SetValue(target, value);
            else pi.SetValue(target, value, null);
        }
        #endregion

        #region MemberInfo
        /// <summary>
        /// Gets/Sets Field or Property info this mapping maps to
        /// </summary>
        public MemberInfo MemberInfo
        {
            get 
            {
                if (fi != null) return fi;
                else return pi;                
            }
            set 
            {
                if (value is FieldInfo) fi = value as FieldInfo;
                else pi = value as PropertyInfo;
            }
        }
        #endregion		
		
        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public ReflectionAttributeMap()
        {
        }
        #endregion
    }
}

